﻿namespace Beetle
{
    using System;
    using System.Net;

    public class IpItem
    {
        private bool _field_a;
        private bool _field_b;
        private byte[] _field_c;
        private byte[] _field_d;
        private string _field_e;

        public IpItem(IPAddress ip)
        {
            this._field_a = true;
            this._field_c = ip.GetAddressBytes();
        }

        public IpItem(IPAddress startip, IPAddress endip)
        {
            this._field_a = true;
            this._field_c = startip.GetAddressBytes();
            this._field_d = endip.GetAddressBytes();
            this._field_b = true;
        }

        public IpItem(IPAddress startip, IPAddress endip, bool permit)
        {
            this._field_a = true;
            this._field_c = startip.GetAddressBytes();
            this._field_d = endip.GetAddressBytes();
            this._field_b = true;
            this._field_a = true;
        }

        public static IpItem From(string ipaddress, bool permit)
        {
            IpItem item = null;
            IPAddress address = null;
            IPAddress address2 = null;
            string[] strArray = ipaddress.Split(new char[] { '-' });
            if (strArray.Length == 2)
            {
                if (IPAddress.TryParse(strArray[0], out address) && IPAddress.TryParse(strArray[1], out address2))
                {
                    item = new IpItem(address, address2, permit) {
                        _field_e = ipaddress
                    };
                }
                return item;
            }
            if (IPAddress.TryParse(strArray[0], out address))
            {
                item = new IpItem(address) {
                    _field_a = permit,
                    _field_e = ipaddress
                };
            }
            return item;
        }

        public bool Verification(IPAddress ip)
        {
            bool flag = false;
            byte[] addressBytes = ip.GetAddressBytes();
            if (this._field_b)
            {
                flag = (((addressBytes[0] >= this._field_c[0]) && ((addressBytes[0] <= this._field_d[0]) & (addressBytes[1] >= this._field_c[1]))) && (((addressBytes[0] <= this._field_d[1]) & (addressBytes[2] >= this._field_c[2])) && ((addressBytes[0] <= this._field_d[2]) & (addressBytes[3] >= this._field_c[3])))) && (addressBytes[0] <= this._field_d[3]);
            }
            else
            {
                flag = (((addressBytes[0] == this._field_c[0]) & (addressBytes[1] == this._field_c[1])) & (addressBytes[2] == this._field_c[2])) & (addressBytes[3] == this._field_c[3]);
            }
            if (this._field_a)
            {
                return flag;
            }
            return !flag;
        }

        public string AddressValue
        {
            get
            {
                return this._field_e;
            }
        }

        public bool Permit
        {
            get
            {
                return this._field_a;
            }
        }
    }
}

